﻿using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore.Query.Internal;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Volo.Abp.Domain.Entities;

namespace Ebusiness_UserService.Model
{
    /// <summary>
    /// 实体中的逻辑代码:管理实体的创建、状态等非业务逻辑。
    /// </summary>
    //[Table("T_User")]
    public class User : AggregateRoot<Guid>
    {
        private User() //efcore使用
        {

        }

        /// <summary>
        /// 实体初始化
        /// </summary>
        /// <param name="phoneNumber"></param>
        public User(PhoneNumber phNumber)  //程序员使用
        {
            this.phoneNumber = phNumber;
            //this.userAccessFail = new UserAccessFail("");
        }

        //public Guid Id { get; set; } = Guid.NewGuid();   // 用户唯一标识
        [MaxLength(200)]
        public string? UserName { get; private set; }             // 登录用户名
        [MaxLength(200)]
        public string? DisplayName { get; set; }          // 显示名称（昵称）
        [MaxLength(200)]
        public string? Email { get; set; }                // 邮箱

        /// <summary>
        /// 非聚合根其他实体
        /// </summary>
        public UserAccessFail userAccessFail { get; private set; }

        /// <summary>
        /// 值对象
        /// </summary>
        [MaxLength(200)]
        public PhoneNumber? phoneNumber { get; private set; }          // 手机号

        //[MaxLength(200)]
        private string? passwordHash;         // 密码哈希

        [MaxLength(200)]
        public string? AvatarUrl { get; set; }            // 头像链接

        public bool IsActive { get; set; } = true;       // 是否启用
        public bool IsDeleted { get; set; } = false;     // 是否逻辑删除
        public DateTime CreatedAt { get; set; } = DateTime.UtcNow;  // 创建时间
        [MaxLength(200)]
        public string? CreatedBy { get; set; }                        // 创建人
        public DateTime? UpdatedAt { get; set; }         // 更新时间
        [MaxLength(200)]
        public string? UpdatedBy { get; set; }            // 更新人

        public DateTime? LastLoginTime { get; set; }     // 最近登录时间

        public void initializeuserAccessFail()
        {
            if (userAccessFail == null)
                this.userAccessFail = new UserAccessFail(false, 0);
        }

        /// <summary>
        /// 状态管理
        /// </summary>
        /// <param name="value"></param>
        /// <exception cref="ArgumentException"></exception>
        public void ChangePassword(string value)
        {
            //修改密码
            //更改实体的状态 可以添加领域事件
            if (value.Length <= 3)
                throw new ArgumentException("密码长度不能小于3");
            //passwordHash = HashHelper.ComputeMdSHash(value);
            passwordHash = value;
        }

        public bool HasPassword()
        {
            if (string.IsNullOrEmpty(passwordHash))
                return false;
            else
                return true;
        }

        /// <summary>
        /// 状态管理
        /// </summary>
        /// <param name="password"></param>
        /// <returns></returns>
        public bool checkPassword(string password)
        {
            //检查密码是否正确
            //return passwordHash == HashHelper.ComputeMdSHash(password);
            return passwordHash == password;
        }

        public void changePhoneNumber(PhoneNumber phoneNumber)
        {
            //修改手机号
            this.phoneNumber = phoneNumber;
        }

        public void ChangeUserName(string UserName)
        {
            if (UserName.Length < 6)
            {
                throw new ArgumentException("长度不能小于6");
            }
            this.UserName = UserName;
        }
    }

    //值对象
    public class PhoneNumber
    {
        public string Code { get; set; }

        public string Phone { get; set; }
    }
}
